9月 7 2017 Online Judge►LeetCode [LeetCode] 515 - Find Largest Value in Each Tree Row 題意找出樹中每一層最大的元素。 解法BFS。 程式12345678910111213141516171819202122232425262728/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {number[]} */var largestValues = function(root) { if ( !root ) return []; let queue = [root]; let result = []; while( queue.length !== 0 ){ let length = queue.length; let max = queue[0].val; for ( let i = 0; i < length; i ++ ){ let node = queue.shift(); if ( node.val > max ) max = node.val; if ( node.left ) queue.push(node.left); if ( node.right ) queue.push(node.right); } result.push(max); } return result;}; Newer [LeetCode] 513 - Find Bottom Left Tree Value Older [LeetCode] 560 - Subarray Sum Equals K